﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for ManageUsers
/// </summary>
public class ManageUsers
{
	public ManageUsers()
	{
	}

    /// <summary>
    /// Lấy danh sách tất cả các tài xế
    /// </summary>
    /// <returns></returns>
    public DataSet getAllUsers()
    {
        DataSet ds = new DataSet();
        SqlConnection con = ManageConnect.OpenConnection();
        try
        {
            SqlDataAdapter da = new SqlDataAdapter("getAllUserNoneAdmind", con.ConnectionString);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.Fill(ds, "Users");
        }
        catch { }
        finally
        {
            ManageConnect.CloseConnection(con);
        }

        return ds;
    }


    /// <summary>
    /// tìm kiếm tất cả các xe bus theo số đăng ký
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public DataSet searchUsersByKeyword(string key)
    {
        DataSet ds = new DataSet();
        SqlConnection con = ManageConnect.OpenConnection();
        try
        {
            SqlDataAdapter sqlDb = new SqlDataAdapter("searchAllUserNoneAdmind", con);
            sqlDb.SelectCommand.CommandType = CommandType.StoredProcedure;

            SqlParameter param = new SqlParameter("@STRING_SEARCH", SqlDbType.NVarChar);
            param.Value = key;
            sqlDb.SelectCommand.Parameters.Add(param);

            sqlDb.Fill(ds, "Users");
        }
        catch (SqlException ex)
        {
            /* ghi ra file log*/

        }
        finally
        {
            ManageConnect.CloseConnection(con);
        }

        return ds;
    }


    public int deleteUser(Users user)
    {
        SqlConnection con = ManageConnect.OpenConnection();

        SqlCommand cmd = new SqlCommand("deleteUser", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@IDUser", SqlDbType.Int).Value = user.IDUser;
     
        SqlParameter result = new SqlParameter("@ReturnValue", SqlDbType.Int);
        result.Direction = ParameterDirection.ReturnValue;
        cmd.Parameters.Add(result);

        try
        {
            cmd.ExecuteNonQuery();
            int val;
            if (int.TryParse(result.Value.ToString(), out val))
            {
                return val;
            }
            else return -1;
        }
        catch (SqlException ex)
        {
            /* ghi ra file log*/
            return -1;
        }
        finally
        {
            ManageConnect.CloseConnection(con);
        }
    }


    public int updateUser(Users user)
    {
        SqlConnection con = ManageConnect.OpenConnection();

        SqlCommand cmd = new SqlCommand("updateUser", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@IDUser", SqlDbType.Int).Value = user.IDUser;
        cmd.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = user.UserName;
        cmd.Parameters.Add("@Pass", SqlDbType.NVarChar).Value = user.Pass;
        cmd.Parameters.Add("@DateOfBirth", SqlDbType.Date).Value = user.DateOfBirth;
        cmd.Parameters.Add("@Phone", SqlDbType.NVarChar).Value = user.Phone;
        cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = user.Email;

        SqlParameter result = new SqlParameter("@ReturnValue", SqlDbType.Int);
        result.Direction = ParameterDirection.ReturnValue;
        cmd.Parameters.Add(result);

        try
        {
            cmd.ExecuteNonQuery();
            int val;
            if (int.TryParse(result.Value.ToString(), out val))
            {
                return val;
            }
            else return -1;
        }
        catch (SqlException ex)
        {
            /* ghi ra file log*/
            return -1;
        }
        finally
        {
            ManageConnect.CloseConnection(con);
        }
    }

    public int insertNeuUser(Users user)
    {
        SqlConnection con = ManageConnect.OpenConnection();

        SqlCommand cmd = new SqlCommand("insertNewUser", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = user.UserName;
        cmd.Parameters.Add("@Pass", SqlDbType.NVarChar).Value = user.Pass;
        cmd.Parameters.Add("@DateOfBirth", SqlDbType.Date).Value = user.DateOfBirth;
        cmd.Parameters.Add("@Phone", SqlDbType.NVarChar).Value = user.Phone;
        cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = user.Email;

        SqlParameter result = new SqlParameter("@ReturnValue", SqlDbType.Int);
        result.Direction = ParameterDirection.ReturnValue;
        cmd.Parameters.Add(result);

        try
        {
            cmd.ExecuteNonQuery();
            int val;
            if (int.TryParse(result.Value.ToString(), out val))
            {
                return val;
            }
            else return -1;
        }
        catch (SqlException ex)
        {
            /* ghi ra file log*/
            return -1;
        }
        finally
        {
            ManageConnect.CloseConnection(con);
        }
    }

    public int loginSystem(String userName,String pass)
    {
        SqlConnection con = ManageConnect.OpenConnection();

        SqlCommand cmd = new SqlCommand("loginSystem", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = userName;
        cmd.Parameters.Add("@Pass", SqlDbType.NVarChar).Value = pass;
        
        SqlParameter result = new SqlParameter("@ReturnValue", SqlDbType.Int);
        result.Direction = ParameterDirection.ReturnValue;
        cmd.Parameters.Add(result);

        try
        {
            cmd.ExecuteNonQuery();
            int val;
            if (int.TryParse(result.Value.ToString(), out val))
            {
                return val;
            }
            else return -1;
        }
        catch (SqlException ex)
        {
            /* ghi ra file log*/
            return -1;
        }
        finally
        {
            ManageConnect.CloseConnection(con);
        }
    }
}